'''
Created on 09/07/2010

@author: Mar
'''

class TripleIndirectionBlock(object):
    
    
    def __init__(self):
        self.maxSize = 32
        self.pointers = []
        
        
    def getPointers(self):
        return self.pointers
    
    def hasSpaceToABlock(self):
        return (self.maxSize > len(self.pointers))    
        
    def addDoubleIndirectionBlock(self, aBlock):
        if (self.hasSpaceToABlock()):
            if(not(self.getPointers().__contains__(aBlock))):
                self.pointers.append(aBlock)
            
    def hasSpaceToASimpleBlock(self):
        for i in self.pointers:
            if i.hasSpaceToABlock():
                return True
        return False
            
    def addSimpleIndirectionBlock(self, aBlock):
        if (self.hasSpaceToASimpleBlock()):
            for i in self.pointers:
                if i.hasSpaceToABlock():
                    i.addIndirectionBlock(aBlock)
                    break
                
            
    def hasSpace(self):
        for i in self.pointers:
            if i.hasSpace:
                return True
        return False
    
    def addDataPointer(self, aPointer, aCounter):
        for i in self.pointers:
            if i.hasSpace():
                i.addDataPointer(aPointer, aCounter)
                
    def removeDoubleIndirectionBlock(self, aBlock):
        if self.pointers.__contains__(aBlock):
            self.getPointers().remove(aBlock)
            
    
    def removeSimpleIndirectionBlock(self, aBlock):
        for i in self.getPointers():
            if i.getPointers().__contains__(aBlock):
                i.removeIndirectionBlock(aBlock)
    
    
                    
    def removeDataPointer(self, aPointer):
        for i in self.getPointers():
            if i.contains(aPointer):
                i.removeDataPointer(aPointer)
                    
    def contains(self, aPointer):
        for i in self.getPointers():
            if i.contains(aPointer):
                return True
        return False
    
    
    def getBlocks(self):
        #=======================================================================
        # Retorna una lista con todos los punteros hacia los que apuntan sus bloques
        #=======================================================================
        listOfBlocks = []
        for i in self.pointers:
            list = i.getBlocks()
            for j in list:
                listOfBlocks.append(j)
        return listOfBlocks
    
    